package com.llkj.qingnianguoxue.util;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarUtil {
    private Calendar calendar;
    private int[] date = new int[42]; // 日历显示数字
    private Date selectedStartDate;
    private Date selectedEndDate;
    private Date curDate; // 当前日历显示的月
    private Date today; // 今天的日期文字显示红色
    private int curStartIndex, curEndIndex; // 当前显示的日历起始的索引
    private Date showFirstDate, showLastDate; // 日历显示的第一个日期和最后一个日期
    private RefreshDateData refreshListener;

    private void init() {
        curDate = selectedStartDate = selectedEndDate = today = new Date();
        calendar = Calendar.getInstance();
        calendar.setTime(curDate);
        calculateDate();
    }

    public CalendarUtil(RefreshDateData refreshListener) {
        super();
        this.refreshListener = refreshListener;
        init();
    }

    private void calculateDate() {
        calendar.setTime(curDate);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int dayInWeek = calendar.get(Calendar.DAY_OF_WEEK);
        Log.d("Log", "day in week:" + dayInWeek);
        int monthStart = dayInWeek;
        if (monthStart == 1) {
            monthStart = 8;
        }
        monthStart -= 1; // 以日为开头-1，以星期一为开头-2
        curStartIndex = monthStart;
        date[monthStart] = 1;
        // last month
        if (monthStart > 0) {
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            int dayInmonth = calendar.get(Calendar.DAY_OF_MONTH);
            for (int i = monthStart - 1; i >= 0; i--) {
                date[i] = dayInmonth;
                dayInmonth--;
            }
            calendar.set(Calendar.DAY_OF_MONTH, date[0]);
        }
        showFirstDate = calendar.getTime();
        // this month
        calendar.setTime(curDate);
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        // Log.d(TAG, "m:" + calendar.get(Calendar.MONTH) + " d:" +
        // calendar.get(Calendar.DAY_OF_MONTH));
        int monthDay = calendar.get(Calendar.DAY_OF_MONTH);
        for (int i = 1; i < monthDay; i++) {
            date[monthStart + i] = i + 1;
        }
        curEndIndex = monthStart + monthDay;
        // next month
        for (int i = monthStart + monthDay; i < 42; i++) {
            date[i] = i - (monthStart + monthDay) + 1;
        }
        if (curEndIndex < 42) {
            // 显示了下一月的
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        calendar.set(Calendar.DAY_OF_MONTH, date[41]);
        showLastDate = calendar.getTime();
        // Log.e("Log", "====---=====showLastDate"+showLastDate);
        // for(int i=0;i<42;i++){
        // Log.e("Log", "====---====="+"date["+i+"]=="+date[i]);
        // }

        if (refreshListener != null) {
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = getDay();
            int[] d = {year, month, day};
            refreshListener.refreshDateData(curStartIndex, curEndIndex, d, date);
        }

    }

    private boolean isLastMonth(int i) {
        if (i < curStartIndex) {
            return true;
        }
        return false;
    }

    private boolean isNextMonth(int i) {
        if (i >= curEndIndex) {
            return true;
        }
        return false;
    }

    // 获得当前应该显示的年月
    public String getYearAndmonth() {
        calendar.setTime(curDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;

        calculateDate();
        if (month < 10) {
            return year + "年0" + month + "月";
        }
        return year + "年" + month + "月";
    }

    // 获得当前应该显示的年月
    public int getDay() {
        calendar.setTime(curDate);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    // 上一月
    public String clickLeftMonth() {
        calendar.setTime(curDate);
        calendar.add(Calendar.MONTH, -1);
        curDate = calendar.getTime();
        return getYearAndmonth();
    }

    // 下一月
    public String clickRightMonth() {
        calendar.setTime(curDate);
        calendar.add(Calendar.MONTH, 1);
        curDate = calendar.getTime();
        return getYearAndmonth();
    }

    public void setYearMonth(int year, int month) {
        calendar.setTime(curDate);
        int m = (year - calendar.get(Calendar.YEAR)) * 12;
        int curM = curDate.getMonth() + 1;
        m = m + month - curM;
        calendar.add(Calendar.MONTH, m);
        curDate = calendar.getTime();
        calculateDate();
    }

    public int getCurrYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    public int getCurrMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1;
    }

    public String getWeek(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        String week = "";
        switch (day) {
            case 2:
                week = "星期一";
                break;
            case 3:
                week = "星期二";
                break;
            case 4:
                week = "星期三";
                break;
            case 5:
                week = "星期四";
                break;
            case 6:
                week = "星期五";
                break;
            case 7:
                week = "星期六";
                break;
            case 1:
                week = "星期日";
                break;
        }
        return week;
    }

    public interface RefreshDateData {
        /**
         * 刷新日历
         *
         * @param curStartIndex 当前显示的日历起始的索引
         * @param curEndIndex
         * @param date
         */
        public void refreshDateData(int curStartIndex, int curEndIndex, int[] day,
                                    int[] date);
    }
}
